//
//  CS_Page_SectionTitleView.m
//  Pods
//
//  Created by 网新中研 on 17/3/1.
//
//

#import "CS_Page_SectionTitleView.h"

@interface CS_Page_SectionTitleView()

@property (nonatomic, strong) UIView *seperatorLine;

@property (nonatomic, assign) SectionTitleType sectionTitleType;


@property (nonatomic, strong) UIView *cellBottomView;

@property (nonatomic, strong) UIView *seperatorView;

@property (nonatomic, strong) UIView *speratorLineView;

@property (nonatomic, strong) UIView *topSeperatorLineView;

@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, copy) RightSectionButtonClickHandler rightClickHandler;
@property (nonatomic, strong) NSString *buttonName;

@property (nonatomic, strong) CS_Page_TextView *content_CS_Page_TextView;

@end

@implementation CS_Page_SectionTitleView

-(NSInteger)viewType{
    return CSViewType_PageSectionTitleView;
}

static NSMutableDictionary *_iconMapping;

/**
 * 添加标题图标类型
 */
+(void)addImageIcon:(SectionTitleType)sectionTitleType iconImage:(UIImage *)iconImage{
    if (_iconMapping == nil) {
        _iconMapping = [NSMutableDictionary new];
    }
    
    [_iconMapping setObject:iconImage forKey:@(sectionTitleType)];
}

//static nsm *_editFocusView;

+(instancetype)page_SectionTitleView:(NSString *)title color:(UIColor *)color image:(UIImage *)image{
    
    CS_Page_SectionTitleView *page_SectionTitleView = [CS_Page_SectionTitleView new];
    page_SectionTitleView.title = title;
    page_SectionTitleView.color = color;
    page_SectionTitleView.image = image;
    
    return page_SectionTitleView;
}

+(instancetype)page_SectionTitleView:(NSString *)title color:(UIColor *)color sectionTitleType:(SectionTitleType)sectionTitleType{
    
    CS_Page_SectionTitleView *page_SectionTitleView = [CS_Page_SectionTitleView new];
    page_SectionTitleView.title = title;
    page_SectionTitleView.color = color;
    page_SectionTitleView.sectionTitleType = sectionTitleType;
    
    return page_SectionTitleView;
}

+(id)page_SectionTitleView:(NSString *)title color:(UIColor *)color image:(UIImage *)image rightButtonName:(NSString *)buttonName rightClickHandler:(RightSectionButtonClickHandler)rightClickHandler{
    
    CS_Page_SectionTitleView *page_SectionTitleView = [CS_Page_SectionTitleView new];
    page_SectionTitleView.title = title;
    page_SectionTitleView.color = color;
    page_SectionTitleView.buttonName = buttonName;
    page_SectionTitleView.rightClickHandler = rightClickHandler;
    
    return page_SectionTitleView;
    
}

+(id)page_SectionTitleView:(NSString *)title Font:(UIFont *)font color:(UIColor *)color image:(UIImage *)image rightButtonName:(NSString *)buttonName rightClickHandler:(RightSectionButtonClickHandler)rightClickHandler{
    
    CS_Page_SectionTitleView *page_SectionTitleView = [CS_Page_SectionTitleView new];
    page_SectionTitleView.title = title;
    page_SectionTitleView.color = color;
    page_SectionTitleView.buttonName = buttonName;
    page_SectionTitleView.rightClickHandler = rightClickHandler;
    page_SectionTitleView.font = font;
    
    return page_SectionTitleView;
    
}

-(instancetype)init{
    if (self = [super init]) {
        self.leftMargin = CELL_EDIT_RIGHT_MARGIN;
        self.rightMargin = CELL_EDIT_RIGHT_MARGIN;
    }
    return self;
}

-(void)refreshTextColor:(UIColor *)textColor{
    [self.content_CS_Page_TextView refreshTextColor:textColor];
}

#pragma mark --- lazy loading

-(UIButton *)rightButton{
    if (_rightButton == nil) {
        _rightButton = [[UIButton alloc] init];
        STYLE_SAVE_BUTTON(_rightButton);
        [_rightButton setTitle:self.buttonName forState:UIControlStateNormal];
        [_rightButton addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _rightButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
    }
    return _rightButton;
}

-(UIView *)cellBottomView{
    if (_cellBottomView == nil) {
        _cellBottomView = [UIView new];
        _cellBottomView.frame = CGRectMake(0, 0, kWindowW, CELL_TOP_MARGIN);
    }
    return _cellBottomView;
}

-(UIView *)seperatorView{
    if (_seperatorView == nil) {
        _seperatorView = [UIView new];
        _seperatorView.backgroundColor = [UIColor whiteColor];
        _seperatorView.frame = CGRectMake(0, CELL_TOP_MARGIN, kWindowW, 10.0);
    }
    return _seperatorView;
}

-(UILabel *)label{
    if (_label == nil) {
        _label = [UILabel new];
        if (self.font) {
            _label.font = self.font;
        }else{
           _label.font = [UIFont boldSystemFontOfSize:16.0];
        }
        _label.textColor = _color;
        _label.text = self.title;
        _label.numberOfLines = 0;
    }
    return _label;
}

-(CS_Page_TextView *)content_CS_Page_TextView{
    if (_content_CS_Page_TextView == nil) {
        UIFont *font = [UIFont boldSystemFontOfSize:FONT_SIZE_2];
        if (self.font) {
            font = self.font;
        }
        _content_CS_Page_TextView = [CS_Page_TextView page_textViewWithFont:font textColor:self.color content:self.title textAlignment:NSTextAlignmentLeft];
        _content_CS_Page_TextView.leftMargin = self.leftMargin;
        _content_CS_Page_TextView.rightMargin = self.rightMargin;
    }
    return _content_CS_Page_TextView;
}

-(UIView *)speratorLineView{
    if (_speratorLineView== nil) {
        _speratorLineView = [[UIView alloc]init];
        _speratorLineView.backgroundColor = COLOR_TOP_SEPERATOR_LINE;
        _speratorLineView.layer.opacity = 0.5;
    }
    return _speratorLineView;
}

-(void)rightButtonClick{
    if (self.rightClickHandler) {
        self.rightClickHandler();
    }
}

-(UIView *)topSeperatorLineView{
    if (_topSeperatorLineView == nil) {
        _topSeperatorLineView = [[UIView alloc] init];
        _topSeperatorLineView.backgroundColor = COLOR_TOP_SEPERATOR_LINE;
        _topSeperatorLineView.layer.opacity = 0.5;
    }
    return _topSeperatorLineView;
}

-(CGFloat)layoutView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    REMOVE_SUBVIEWS(self);
    
    //判断是否为空
    if([self isNullOrEmpty:self.title]){
        
        UIView *seperateView = [[UIView alloc] init];
        seperateView.backgroundColor = [UIColor colorFromHexCode:@"e7e7e7"];
        seperateView.frame = CGRectMake(0, 0, kWindowW, 10);
        [self addSubview:seperateView];
        
        self.frame = CGRectMake(0, primaryY, kWindowW, 10);
        [superView addSubview:self];
        
        return primaryY + 10;
        
    }
    
    CGFloat originY = primaryY;
    
    //标题部分
    CGFloat seperatorViewHeight = 0;
    
    CGFloat finalHeight = 0;
    
    if(self.image){
        
        self.content_CS_Page_TextView.leftMargin = CELL_EDIT_RIGHT_MARGIN + 22 + 10;
        
        finalHeight = [self.content_CS_Page_TextView layoutView:self primaryY:seperatorViewHeight + 1 + 15];
        
        CGFloat primaryImageY = (finalHeight - 22)/2.0;
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:self.image];
        imageView.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, primaryImageY, 22, 22);
        [self addSubview:imageView];
        
    }else{
        
        finalHeight = [self.content_CS_Page_TextView layoutView:self primaryY:seperatorViewHeight + 1 + 15];
        
    }
    
//    finalHeight = finalHeight > 20.0 ? finalHeight : 20.0;
//    finalHeight += 2 * 11;
//    UIImage* localimage = [self getSectionImage];
//    if (localimage) {
//        _image = localimage;
//    }
//    if ( _image == nil){
//        
//        self.label.frame = CGRectMake(self.leftMargin, seperatorViewHeight + 1, kWindowW - self.leftMargin - self.rightMargin, finalHeight - 1);
//        
//    }else{
//        
//        UIImageView *imageView = [[UIImageView alloc]initWithImage:_image];
//        imageView.frame = CGRectMake(15, seperatorViewHeight + 11.0, 28, 28);
//        imageView.tintColor = COLOR_THEME;
//        [self addSubview:imageView];
//        self.label.frame = CGRectMake(CELL_SEPERATOR_MARGIN_LEFT + 45.0, seperatorViewHeight, kWindowW - CELL_SEPERATOR_MARGIN_LEFT - CELL_RIGHT_MARGIN, finalHeight);
//        
//    }
//    [self addSubview:self.label];
    
    if (self.sectionItems.count > 0) {
        self.speratorLineView.frame = CGRectMake(0, seperatorViewHeight + finalHeight + 1, kWindowW - 2 * 10, 1.0);
        [self addSubview:self.speratorLineView];
    }
    
    NSInteger viewIndex = superView.subviews.count;
    
    primaryY = primaryY + CELL_CONTENT_MARGIN_HEIGHT + CELL_CONTENT_MARGIN_HEIGHT + finalHeight + CELL_SEPERATOR_LINE_HEIGHT +1 + 10;
    
    //部署子视图
    for (int i = 0; i < self.sectionItems.count; i++) {
        CSBaseView *view = (CSBaseView *)[self.sectionItems objectAtIndex:i];
        primaryY = [view layoutView:superView primaryY:primaryY];
    }
    
    if (self.sectionItems.count > 0) {
        self.frame = CGRectMake(10, originY + 20, kWindowW - 20, primaryY - originY - 10 + 1 - 5);
    }else{
        
        if (self.isTopSeperatorLineHide) {
            self.frame = CGRectMake(0, originY, kWindowW - 0, primaryY - originY - 36);
        }else{
            UIView *seperatorView = [[UIView alloc] init];
            seperatorView.backgroundColor = COLOR_TOP_SEPERATOR_LINE;
            seperatorView.frame = CGRectMake(0, -10, kWindowW, 10);
            [self addSubview:seperatorView];
            self.frame = CGRectMake(0, originY + 10, kWindowW - 0, primaryY - originY - 36);
        }
        
        self.speratorLineView.backgroundColor = COLOR_TOP_SEPERATOR_LINE;
        self.speratorLineView.frame = CGRectMake(0, self.frame.size.height - 1, kWindowW, 1);
        self.topSeperatorLineView.frame = CGRectMake(0, 0, kWindowW, 1);
        
        if (![self isNullOrEmpty:self.buttonName]) {
            CGFloat needWidth = [self estimateWith:self.buttonName font:[UIFont systemFontOfSize:15.0] height:20] + 10;
            self.rightButton.layer.cornerRadius = 2;
            self.rightButton.frame = CGRectMake(kWindowW - CELL_EDIT_RIGHT_MARGIN - needWidth, 12, needWidth, 26);
            [self addSubview:self.rightButton];
        }
        
        [self addSubview:self.speratorLineView];
        [self addSubview:self.topSeperatorLineView];
        
    }
    
    [superView insertSubview:self atIndex:viewIndex];
    
    self.backgroundColor = [UIColor whiteColor];
    
    return originY + primaryY - originY - 36 + 10;
}

-(UIImage *)getSectionImage{
    if (_iconMapping != nil) {
        return [_iconMapping objectForKey:@(_sectionTitleType)];
    }
    return nil;
}

-(BOOL)isCornerRadusRequired{
    return YES;
}

-(NSMutableArray *)sectionItems{
    if (_sectionItems == nil) {
        _sectionItems = [NSMutableArray new];
    }
    return _sectionItems;
}

+ (instancetype)mj_refreshBundle
{
    static NSBundle *refreshBundle = nil;
    if (refreshBundle == nil) {
        // 这里不使用mainBundle是为了适配pod 1.x和0.x
        refreshBundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[CSBaseView class]] pathForResource:@"CSUIKitResource" ofType:@"bundle"]];
    }
    return refreshBundle;
}

+ (UIImage *)mj_arrowImage
{
    static UIImage *arrowImage = nil;
    if (arrowImage == nil) {
        arrowImage = [[UIImage imageWithContentsOfFile:[[self mj_refreshBundle] pathForResource:@"arrow@2x" ofType:@"png"]] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    }
    return arrowImage;
}

- (UIView *)seperatorLine {
    if(_seperatorLine == nil) {
        _seperatorLine = [[UIView alloc] init];
        STYLE_SEPERATOR_LINE(_seperatorLine);
    }
    return _seperatorLine;
}

@end
